﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Views;
using IService.Registration;
using Microsoft.Practices.ServiceLocation;
using Model.Common.ValidateTextBox;
using System.Text.RegularExpressions;
using System.Windows.Input;

namespace ViewModel.Registration
{
    public class RegistrationViewModel:ViewModelBase
    {
        public RegistrationViewModel(IRegistrationService service)
        {
            _service = service;
            PasswordValidator = new SimpleValidator((str, param) =>
              {
                  if ((Regex.IsMatch(str, "[A-Z]") == false) ||
                       (Regex.IsMatch(str,"[a-z]")==false) ||
                       (Regex.IsMatch(str,"[0-9]")==false))
                      return new ValidateResult(false, "密码必须包含至少一个大写字母,一个小写字母和一个数字");
                  return new ValidateResult(true, null);
              });
            RegistrationCommand = new RelayCommand(Registration);
        }
        #region 服务
        IRegistrationService _service;
        #endregion
        #region 属性
        private string _userName;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
                RaisePropertyChanged(() => UserName);
            }
        }

        private string _nickName;
        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName
        {
            get
            {
                return _nickName;
            }
            set
            {
                _nickName = value;
                RaisePropertyChanged(() => NickName);
            }
        }


        private string _password1;
        /// <summary>
        /// 密码1
        /// </summary>
        public string Password1
        {
            get
            {
                return _password1;
            }
            set
            {
                _password1 = value;
                RaisePropertyChanged(() => Password1);
            }
        }


        private string _password2;
        /// <summary>
        /// 密码2
        /// </summary>
        public string Password2
        {
            get
            {
                return _password2;
            }
            set
            {
                _password2 = value;
                RaisePropertyChanged(() => Password2);
            }
        }

        private ValidatorBase _passwordValidator;
        /// <summary>
        /// 密码验证器
        /// </summary>
        public ValidatorBase PasswordValidator
        {
            get
            {
                return _passwordValidator;
            }
            set
            {
                _passwordValidator = value;
                RaisePropertyChanged(() => PasswordValidator);
            }
        }

        private string _sex;
        /// <summary>
        /// 密码2
        /// </summary>
        public string Sex
        {
            get
            {
                return _sex;
            }
            set
            {
                _sex = value;
                RaisePropertyChanged(() => Sex);
            }
        }
        #endregion
        #region 命令
        public ICommand RegistrationCommand { get; set; }
        private void Registration()
        {
            string result = _service.Registration(UserName, NickName, Password1, Sex);
            if(result!=null)
            {
                ServiceLocator.Current.GetInstance<IDialogService>().ShowError(result, "注册失败", null, null);
            }
            else
            {
                ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("注册成功!", null);
                MessengerInstance.Send<object>(null, "RegistrationViewModel_Registration_Success");
            }
        }

        #endregion
        #region 清理
        public override void Cleanup()
        {
            base.Cleanup();
            UserName = null;
            NickName = null;
            Password1 = null;
            Password2 = null;
            Sex = "男";
        }
        #endregion
    }
}
